<cfsetting enablecfoutputonly="Yes">
<!---
	where/vote.cfm
--->

<cfset place=structNew()>
<cfloop list="lat,lng,street,title,city,region,country,phone,amenitiesrating,overallrating,quietrating,fooddrinks,foodsolid,wififree,wifipaid,outlets" index="colName">
	<cfif structKeyExists(Form, colName)>
		<cfset place[colName]=trim(Form[colName])>
	<cfelse>
		<cfset place[colName]="">
	</cfif>
</cfloop>
<cfmodule template="_head.cfm" pageTitle="Place">

<cfoutput>
<style type="text/css">
html, body {
	background-color: white;
	color: black;
	font-family: Calibri, Tahoma, Verdana, Arial, 'Bitstream Vera Sans', 'Vera Sans', Helvetica, sans-serif, sans;
	padding: 0;
	margin: 0;
}
p {
	margin: 0 0 0.5em 0;
}
.err {
	color: maroon;
	background-color: yellow;
	padding: 1em;
	text-align: center;
}
h1 {
	font-size: 115%;
	color: ##224488;
	margin: 0 0 0.25em 0;
	padding: 0 0 0 0;
	border-bottom: 1px solid ##3366cc;
	font-weight: bold;
}
h2 {
	font-size: 100%;
	font-weight: bold;
	color: ##666666;
	margin: 0 0 0 0;
	padding: 0 0 0 0;
	border-top: 1px solid ##dddddd;
	background-color: ##f8f8f8;
	padding-left: 0.25em;
}
.tel { float: right; }
.info {
	border-top: 1px solid ##eeeeee;
	border-bottom: 1px solid ##eeeeee;
	background-color: ##f8f8f8;
	color: ##4488ff;
	padding: 0.5em;
	text-align: center;
}
.adr { margin-bottom: 0.25em; }
td {
	padding: 0.125em 0.25em;
}
.selected { color: ##333333; background-color: ##dddddd; }
.yes .selected { color: ##006600; background-color: ##ddffdd; }
.no .selected { color: ##660000; background-color: ##ffdddd; }
span.yes { color: ##006600; }
span.no { color: ##660000; }
span.unsure { color: ##999999; }
form {
	margin: 0 0 0 0;
	padding: 0 0 0 0;
}
.radio input { float: left; }
.radio label { display: block; }
.ratingcounts td { color: ##cccccc; }
.rating {
	width: 75px;
	height: 14px;
	background: url(stars75-back.png) top left;
	overflow: hidden;
	display: block;
	border: none;
	text-decoration: none;
	text-align: left;
}
.rating div {
	height: 14px;
	width: 0px;
	background: url(stars75-front.png) top left;
	overflow: hidden;
}
</style>
</head>
<body>
</cfoutput>

<cftry>
	<cfif not (isNumeric(place.lat) and isNumeric(place.lng) and (len(place.title) gt 0))><cfthrow message="Your browser seems to be sending us garbage.  Please reload the page."></cfif>
	<cflock name="nwsAddVote" timeout="5" type="EXCLUSIVE">
		<cftransaction>
			<cfquery datasource="nowrists2007" name="FindPlace">
			SELECT id
			FROM nws_places
			WHERE (lat = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Int(place.lat*1000000)#">)
			  AND (lng = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Int(place.lng*1000000)#">)
			  AND (title = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.title,64)#">)
			</cfquery>
			<cfif FindPlace.RecordCount eq 0>
				<cfquery datasource="nowrists2007">
				INSERT INTO nws_places (Lat, Lng, Title, Street, City, Region, Country, Phone)
				VALUES (
					<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Int(place.lat*1000000)#">,
					<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Int(place.lng*1000000)#">,
					<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#left(place.title,64)#">,
					<cfif place.street neq ""><cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.street,64)#"><cfelse>NULL</cfif>,
					<cfif place.city neq ""><cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.city,64)#"><cfelse>NULL</cfif>,
					<cfif place.region neq ""><cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.region,16)#"><cfelse>NULL</cfif>,
					<cfif place.country neq ""><cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.country,16)#"><cfelse>NULL</cfif>,
					<cfif place.phone neq ""><cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.phone,16)#"><cfelse>NULL</cfif>
				)
				</cfquery>
				<cfquery datasource="nowrists2007" name="FindPlace">
				SELECT id
				FROM nws_places
				WHERE (lat = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Int(place.lat*1000000)#">)
				  AND (lng = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Int(place.lng*1000000)#">)
				  AND (title = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#Left(place.title,64)#">)
				</cfquery>
			</cfif>
			<cfif FindPlace.RecordCount eq 0>
				<cfthrow message="It looks like our web server is having problems.  Can you please try again later?">
			</cfif>
			<cfquery datasource="nowrists2007">
			DELETE
			FROM nws_place_votes
			WHERE (placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">)
			  AND (ip = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#CGI.REMOTE_ADDR#">)
			</cfquery>
			<cfquery datasource="nowrists2007">
			INSERT INTO nws_place_votes (placeId, ip, OverallRating, QuietRating, AmenitiesRating, WifiFree, WiFiPaid, FoodDrinks, FoodSolid, Outlets)
			VALUES (
				<cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">,
				<cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#CGI.REMOTE_ADDR#">,
				<cfif isNumeric(place.overallRating) and (place.overallRating gte 0) and (place.overallRating lte 5)><cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Round(place.overallRating * 50)#"><cfelse>NULL</cfif>,
				<cfif isNumeric(place.quietRating) and (place.quietRating gte 0) and (place.quietRating lte 5)><cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Round(place.quietRating * 50)#"><cfelse>NULL</cfif>,
				<cfif isNumeric(place.amenitiesRating) and (place.amenitiesRating gte 0) and (place.amenitiesRating lte 5)><cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#Round(place.amenitiesRating * 50)#"><cfelse>NULL</cfif>,
				<cfif place.WifiFree eq "yes">1<cfelseif place.wifiFree eq "no">0<cfelse>NULL</cfif>,
				<cfif place.WifiPaid eq "yes">1<cfelseif place.wifiPaid eq "no">0<cfelse>NULL</cfif>,
				<cfif place.FoodDrinks eq "yes">1<cfelseif place.FoodDrinks eq "no">0<cfelse>NULL</cfif>,
				<cfif place.FoodSolid eq "yes">1<cfelseif place.FoodSolid eq "no">0<cfelse>NULL</cfif>,
				<cfif place.Outlets eq "yes">1<cfelseif place.Outlets eq "no">0<cfelse>NULL</cfif>
			)
			</cfquery>
			<cfquery datasource="nowrists2007">
			UPDATE nws_places
			SET
			  overallrating = ( SELECT AVG(overallRating) FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">),
			  overallratingcount = ( SELECT SUM(CASE WHEN overallrating IS NOT NULL THEN 1 ELSE 0 END) FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">),
			  quietrating = ( SELECT AVG(quietRating) FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">),
			  quietratingcount = ( SELECT SUM(CASE WHEN quietrating IS NOT NULL THEN 1 ELSE 0 END) FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">),
			  amenitiesrating = ( SELECT AVG(amenitiesRating) FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">),
			  amenitiesratingcount = ( SELECT SUM(CASE WHEN amenitiesrating IS NOT NULL THEN 1 ELSE 0 END) FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">),
			  wififree = ( SELECT CASE WHEN (y >= n) AND (y >= u) THEN 1 WHEN (n >= y) AND (n >= u) THEN 0 END FROM ( SELECT SUM(CASE WHEN wififree = 1 THEN 1 ELSE 0 END) AS y, SUM(CASE WHEN wififree = 0 THEN 1 ELSE 0 END) AS n, SUM(CASE WHEN wififree IS NULL THEN 1 ELSE 0 END) AS u FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#"> ) AS a ),
			  wifipaid = ( SELECT CASE WHEN (y >= n) AND (y >= u) THEN 1 WHEN (n >= y) AND (n >= u) THEN 0 END FROM ( SELECT SUM(CASE WHEN wifipaid = 1 THEN 1 ELSE 0 END) AS y, SUM(CASE WHEN wifipaid = 0 THEN 1 ELSE 0 END) AS n, SUM(CASE WHEN wifipaid IS NULL THEN 1 ELSE 0 END) AS u FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#"> ) AS a ),
			  foodsolid = ( SELECT CASE WHEN (y >= n) AND (y >= u) THEN 1 WHEN (n >= y) AND (n >= u) THEN 0 END FROM ( SELECT SUM(CASE WHEN foodsolid = 1 THEN 1 ELSE 0 END) AS y, SUM(CASE WHEN foodsolid = 0 THEN 1 ELSE 0 END) AS n, SUM(CASE WHEN foodsolid IS NULL THEN 1 ELSE 0 END) AS u FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#"> ) AS a ),
			  fooddrinks = ( SELECT CASE WHEN (y >= n) AND (y >= u) THEN 1 WHEN (n >= y) AND (n >= u) THEN 0 END FROM ( SELECT SUM(CASE WHEN fooddrinks = 1 THEN 1 ELSE 0 END) AS y, SUM(CASE WHEN fooddrinks = 0 THEN 1 ELSE 0 END) AS n, SUM(CASE WHEN fooddrinks IS NULL THEN 1 ELSE 0 END) AS u FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#"> ) AS a ),
			  outlets = ( SELECT CASE WHEN (y >= n) AND (y >= u) THEN 1 WHEN (n >= y) AND (n >= u) THEN 0 END FROM ( SELECT SUM(CASE WHEN outlets = 1 THEN 1 ELSE 0 END) AS y, SUM(CASE WHEN outlets = 0 THEN 1 ELSE 0 END) AS n, SUM(CASE WHEN outlets IS NULL THEN 1 ELSE 0 END) AS u FROM nws_place_votes WHERE placeId = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#"> ) AS a )
			WHERE (id = <cfqueryparam cfsqltype="CF_SQL_INTEGER" value="#FindPlace.id#">)
			</cfquery>
		</cftransaction>
	</cflock>
	<cfoutput>
<h1 class="label">#HTMLEditFormat(place.title)#</h1>
<!--- 
<div class="tel">#HTMLEditFormat(place.phone)#</div>
<div class="adr">
<div class="street-address">#HTMLEditFormat(place.street)#</div>
<div class="city-state-country">
	<span class="locality">#HTMLEditFormat(place.city)#</span>, <span class="region">#HTMLEditFormat(place.region)#</span> <span class="country-name">#HTMLEditFormat(place.country)#</span>
</div>
</div>
--->
	</cfoutput>
	<cfset place.titlenoformatting=place.title>
	<cfset structDelete(place,"title")>
	<cfset place.streetaddress=place.street>
	<cfset structDelete(place,"street")>
	<cfoutput>
<p class="info">Thank you for letting others know what you think of this place!</p>
<p align="center"><a href="place.cfm?<cfloop collection="#place#" item="colName">&amp;#urlEncodedFormat(lCase(colName))#=#urlEncodedFormat(place[colName])#</cfloop>" target="_self">View the updated information.</a></p>
	</cfoutput>
<cfcatch>
	<cfoutput><p class="err">#CFCatch.Message#<br/>#cfcatch.detail#</p></cfoutput>
</cfcatch>
</cftry>

<cfoutput>
</body>
</cfoutput>


<cfsetting enablecfoutputonly="No">